package org.jeecg.modules.app.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.api.announcement.entity.Announcement;
import org.jeecg.modules.api.announcement.service.IAnnouncementService;
import org.jeecg.modules.api.levelexplain.service.ILevelExplainService;
import org.jeecg.modules.api.member.entity.Member;
import org.jeecg.modules.api.member.service.IMemberService;
import org.jeecg.modules.api.order.service.IOrderService;
import org.jeecg.modules.api.video.entity.Video;
import org.jeecg.modules.api.video.service.IVideoService;
import org.jeecg.modules.app.service.IIndexService;
import org.jeecg.modules.app.service.ITeamService;
import org.jeecg.modules.app.vo.IndexVo;
import org.jeecg.modules.helper.MemberHelp;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 首页service实现类
 * @author: Young Jun
 * @date: 2024年09月20日 10:51
 */
@Service
public class IndexServiceImpl implements IIndexService {

    @Autowired
    private IMemberService memberService;
    @Autowired
    private ILevelExplainService levelExplainService;
    @Autowired
    private IAnnouncementService announcementService;
    @Autowired
    private IVideoService videoService;
    @Autowired
    private ITeamService teamService;
    @Autowired
    private MemberHelp memberHelp;
    @Autowired
    private IOrderService orderService;

    @Override
    public IndexVo getIndex() {
        IndexVo vo = new IndexVo();
        Member member = memberHelp.getMember();
        vo.setMemberInfo(member);
        Announcement announcement = announcementService.getOne(new LambdaQueryWrapper<Announcement>()
                .eq(Announcement::getIsDisplay, 1).eq(Announcement::getType,0)
                .last("limit 1"));
        if (announcement != null) {
            vo.setAnnouncement(announcement);
        }

        Video video = videoService.getOne(new LambdaQueryWrapper<Video>()
                .eq(Video::getIsDisplay, 1)
                .last("limit 1"));
        if (video != null) {
            vo.setVideoInfo(video);
        }
        //推荐人信息
        if (member.getRecommendId() != null) {
            Member recommend = memberService.getById(member.getRecommendId());
            if (recommend != null) {
                vo.setRecommendInfo(recommend);
            }
        }

        //分销订单数量
        // orderService.c
        int orderNum = orderService.getFxOrderNum(member.getId());
        vo.setOrderNum(orderNum);
        //我的团队
        int team1 = 0;
        int team2 = 0;
        List<Member> myTeam = teamService.getMyTeam(null);
        if (myTeam != null) {
            team1 = myTeam.size();
            if (team1 > 0) {
                for (Member teamEntity : myTeam) {
                    if (teamEntity.getChildList() != null) {
                        team2 += teamEntity.getChildList().size();
                    }
                }
            }
        }
        vo.setTeamNum(team1 + team2);
        //佣金排行榜
        int fyNum=100;
        List<Member>membersList = memberService.getRankList(100);
        for(int i=0;i<membersList.size();i++){
            Member m=membersList.get(i);
            if(m.getId()==member.getId()){
                fyNum = i+1;
                break;
            }
        }
        vo.setFyNum(fyNum);

        System.out.println("Index--->"+ JSONObject.toJSONString(vo));
        return vo;
    }

    @Override
    public IPage<Announcement> getAnnouncement(Integer type,Integer pageNo, Integer pageSize) {
        LambdaQueryWrapper<Announcement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Announcement::getIsDisplay,1);
        queryWrapper.eq(Announcement::getType,type);
        queryWrapper.orderByDesc(Announcement::getCreateTime);
        Page<Announcement> page = new Page<Announcement>(pageNo, pageSize);
        return announcementService.page(page, queryWrapper);
    }
}
